package yxy.game.pm2.active.module;

import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.vipdata;
import yxy.game.pm2.active.module.subordinateclass.VIPgoods;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * vip
 *
 * @author Dell
 */
public class ActiveModule_VIP extends PlayerModule {

    //bean ------------------------------------

    //vip 道具
    private ArrayList<VIPgoods> vipgoodss;

    public ArrayList<VIPgoods> getVipgoodss() {
        return vipgoodss;
    }

    public void setVipgoodss(ArrayList<VIPgoods> vipgoodss) {
        this.vipgoodss = vipgoodss;
    }

    //fun ------------------------------------

    public static HashMap<Integer, Integer> lvexpmap = new HashMap<Integer, Integer>();

    public HashMap<Integer, Integer> gainlvexpmap() {
        if (lvexpmap.isEmpty()) {
            Map<String, vipdata> vdmap = DataCenter.getAllData(vipdata.class);
            for (String key : vdmap.keySet()) {
                vipdata vd = vdmap.get(key);
                lvexpmap.put(vd.getViplv(), vd.getVipexp() - vd.getNeedexp());
            }
        }
        return lvexpmap;
    }

    static public ActiveModule_VIP create(String playerCode) {
//		int needexp = 60;
//		vipdata vd = DataCenter.getData(0, vipdata.class);
//		if(vd != null) {
//			needexp = vd.getNeedexp();
//		}

        ActiveModule_VIP am = new ActiveModule_VIP();
        am.setPlayerCode(playerCode);
        am.setVipgoodss(new ArrayList<VIPgoods>());
        am.set();
        return am;
    }

    public void handlebuyvipgoods(int vlv) {
        VIPgoods vg = new VIPgoods();
        vg.setViplv(vlv);
        vg.setBuytime(DateTime.now());
        ArrayList<VIPgoods> vgs = getVipgoodss();
        if (vgs == null) {
            vgs = new ArrayList<VIPgoods>();
        }
        vgs.add(vg);
        setVipgoodss(vgs);
        this.set();
    }

    /**
     * 已经购买的vip特权商品list
     *
     * @return
     */
    public ArrayList<Integer> hadbuyvipgoods() {
        ArrayList<Integer> bvgs = new ArrayList<Integer>();
        ArrayList<VIPgoods> vgs = getVipgoodss();

        if (vgs == null) {
            return new ArrayList<Integer>();
        }

        for (int i = 0; i < vgs.size(); i++) {
            VIPgoods vg = vgs.get(i);
            bvgs.add(vg.getViplv());
        }

        return bvgs;
    }

    public void checkvip(JSONObject json, int viplevel, int vipexp) {
        vipdata vd = DataCenter.getData(viplevel, vipdata.class);

        if (vd.getNeedexp() == 0) {
            return;
        }

        if (vipexp >= vd.getNeedexp()) {
            viplevel = viplevel + 1;
            vipexp = vipexp - vd.getNeedexp();

            json.put("newviplevel", viplevel);
            json.put("newvipexp", vipexp);

            vd = DataCenter.getData(viplevel, vipdata.class);
            if (vipexp >= vd.getNeedexp()) {
                checkvip(json, viplevel, vipexp);
            }
        }

    }

    //redis ------------------------------------

    private static String key(String pcode) {
        return String.format("PLAYER:%s:VIP", pcode);
    }

    public void set() {
        try {
            String key1 = key(getPlayerCode());
            setv(key1, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static public ActiveModule_VIP get(String pcode, Boolean isNewPlayer) {
        try {
            ActiveModule_VIP module = getv(key(pcode));
            if (module == null) {
                //新用户不需要去查数据库
                if (!isNewPlayer) {
                    module = MySQLCacheDAO.getV(key(pcode), ActiveModule_VIP.class);
                    if (module != null) {
                        module.set();
                    }//同步到redis
                }

                if (module == null) {
                    module = create(pcode);
                }
            }
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                ActiveModule_VIP activeModule_VIP = getv(key(playerCode));
                MySQLCacheDAO.setV(key, activeModule_VIP);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    // lock==========================================================================
    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

}
